/**

* <p>Title: OrdersServiceImpl.java</p>

* <p>Description: </p>

* <p>Copyright: Copyright (c) 2021</p>

* <p>Company: www.chinasofti.com</p>

* @author knowno

* @date 2021年1月16日

* @version 1.0

*/
package com.etc.zmall.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.etc.dao.ItemsMapper;
import com.etc.dao.OrderMapper;
import com.etc.dao.ShopcMapper;
import com.etc.entity.Items;
import com.etc.entity.Order;
import com.etc.entity.OrderExample;
import com.etc.zmall.service.OrdersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 
 * <p>
 * Title: OrdersServiceImpl
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * @author knowno
 * 
 * @date 2021年1月16日
 * 
 */
@Service
public class OrdersServiceImpl implements OrdersService {

	// 订单
	@Autowired
	private OrderMapper orderMapper;

	// 订单项
	@Autowired
	private ItemsMapper itemsMapper;

	// 购物车
	@Autowired
	private ShopcMapper shopcMapper;

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: addOrder
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param orders
	 * @return
	 * 
	 * @see com.etc.zmall.service.OrdersService#addOrder(com.etc.zmall.entity.Orders)
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = RuntimeException.class)
	public boolean addOrder(Order orders) {

		boolean lastresult = false;
		// 1. 添加订单
		int result = orderMapper.insert(orders);
		if (result == 1) {
			// 2. 添加订单详情
			boolean flag = addOrderItems(orders.getItems());
			if (flag) {
				//// 3. 删除购物车对应记录 --尚未编写
				flag = deleteShopcItem(orders.getShopcids());
				if (flag) {
					lastresult = true;
				}
			}
		}
		return lastresult;

	}

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: addOrderItems
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param items
	 * @return
	 * 
	 * @see com.etc.zmall.service.OrdersService#addOrderItems(java.util.List)
	 */
	public boolean addOrderItems(List<Items> items) {
		int result = itemsMapper.insertBatch(items);
		// 手动引发异常
		// System.out.println(1 / 0); 
		return result == items.size();
	}

	/**
	 * (non-Javadoc)
	
	* <p>Title: deleteShopcItem</p>
	
	* <p>Description: </p>
	
	* @param list
	* @return
	
	* @see com.etc.zmall.service.OrdersService#deleteShopcItem(java.util.List)
	 */
	public boolean deleteShopcItem(List<Integer> list) {
		int result = shopcMapper.deleteByMulti(list);
		return result == list.size();
	}

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: upOrdersState
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param orderstate
	 * @param orderid
	 * @return
	 * 
	 * @see com.etc.zmall.service.OrdersService#upOrdersState(int, java.lang.String)
	 * 
	 */
	@Override
	public boolean upOrdersState(int orderstate, String orderid) {
		// TODO Auto-generated method stub
		Order order = new Order();
		order.setOrderid(orderid);
		order.setOrderstate(orderstate);
		return orderMapper.updateByPrimaryKeySelective(order) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: getByPage
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param keywords
	 * @param begindate
	 * @param enddate
	 * @param pageNo
	 * @param pageSize
	 * @return
	 * 
	 * @see com.etc.zmall.service.OrdersService#getByPage(java.lang.String,
	 *      java.lang.String, java.lang.String, int, int)
	 * 
	 */
	@Override
	public PageInfo<Order> getByPage(String keywords, String begindate, String enddate, int page, int limit) {
		// TODO Auto-generated method stub
		if (page < 1) {
			page = 1;
		}
		// 创建example对象
		OrderExample example = new OrderExample();

		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");

		if ("".equals(enddate)) {
			enddate = new Date().toLocaleString();
		}

		// 判断begindate是否为""
		if ("".equals(begindate)) {
			// 结束时间可以为null,或者结束结束时间不为空
			example.createCriteria().andOrderidLike("%" + keywords + "%");
		} else {
			try {

				// 模糊查询 //时间范围的检索
				example.createCriteria().andOrderidLike("%" + keywords + "%")
						.andOrderdateBetween(simple.parse(begindate), simple.parse(enddate));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// 分页查询
		PageHelper.startPage(page, limit);
		List<Order> list = orderMapper.selectByExample(example);

		// 返回一个pageInfo对象包含了分页有关的各种属性，相当于我们以前的pageData
		PageInfo<Order> pd = new PageInfo<Order>(list);
		return pd;
	}
}
